var utils= {
    ajaxError:function(json,errorCallback,tips) {
        var result = json||{code:500,message:'请求发生错误, 请稍后再试.'};
        if(tips){
            alert(result.message);
        }
        errorCallback && (errorCallback(result));
    },
    ajax:function(opts,callback, errorCallback){
        var url = urlFormat(opts.url || '');
        var noTips = opts.noTips || false;

        $.ajax({
            type: opts.method || 'POST',
            url: url,
            contentType:  opts.type == 'POST' ? 'application/x-www-form-urlencoded' : 'application/json;charset=UTF-8',
            headers: {},
            data: opts.method == 'GET' ? null : JSON.stringify(opts.data || {}),
            processData:false,
            dataType: 'json',
            beforeSend: function(req){

            },
            success: function(json){
                if(json.code==200){
                    callback(json.total==undefined?json.data:{data:json.data,total:json.total});
                }else{
                    utils.ajaxError(json,errorCallback,!noTips);
                }
            },
            error: function(req, status, e) {
                utils.ajaxError(null,errorCallback,!noTips);
            },
            complete: function(req,status){

            }
        });

        function urlFormat(url) {
            var params = '';
            var exists = url.indexOf('?') > -1;
            for (var key in (opts.urlData || {})) {
                params += (!exists && params.length == 0 ? '?' : '&') + key + '=' + (opts.urlData[key] || '');
            }
            return url += params;
        }

    },
    json:function(options,callback, errorCallback){
        (!options) && (options = {})
        utils.ajax(options, callback, errorCallback);
    },
    post:function(options, callback, errorCallback) {
        (!options) && (options = {})
        options.type = 'POST';
        utils.ajax(options, callback, errorCallback);
    },
    get:function(options, callback, errorCallback) {
        (!options) && (options = {})
        options.method = 'GET';
        options.type = 'POST';
        utils.ajax(options, callback, errorCallback);
    }
};